/* 
    分析所有.txt文件中的链接，将职位要求存储于.detail.txt文件
*/


const fs = require('fs');
const { resolve } = require('path');
const { reqDetails } = require('./getDetails.js');
const env = require(resolve('./config.js'));
const dp = env[env.mode];
let reg = /<dd data-selector="job-intro-content">(.*?)<\/dd>/gs;
let tmp;

let url = resolve('./out/front end/front end_');

(async function readAll() {
    for (let item of dp) {
        if (item.code == '050020') {
            let fullUrl = url + `${item.code}.detail.txt`;
            let ws = fs.createWriteStream(fullUrl);
            let res = fs.readFileSync(url + `${item.code}.txt`).toString().split('\n');
            await readDetail(res, 0, fullUrl, ws);
        }
    }
})()


function readDetail(res, count, fullUrl, ws) {
    return new Promise(function (resolve, reject) {
        (function read(res, count, fullUrl, ws) {
            let reg = /<dd data-selector="job-intro-content">(.*?)<\/dd>/gs;
            if (count == 0) {
                console.log('开始读取链接');
            }
            if (count >= res.length - 1) {
                console.log('全部读取完毕');
                ws.close();
                resolve();
                return;
            }
            new Promise(function (resolve, reject) {

                reqDetails(res[count]).then(
                    value => {
                        try {
                            tmp = reg.exec(value);
                            if (tmp == null) throw new Error('正则匹配失败');
                            ws.write(tmp[1] + '\n');
                            console.log(`${fullUrl}第${count}条链接读取成功!`);
                            resolve();
                        } catch (e) {
                            console.error(e.message);
                            resolve();
                        }
                    },
                    reason => {
                        console.log(`${fullUrl}第${count}条链接读取错误:`+reason);
                    }
                )

            }).then(() => {
                setTimeout(() => {
                    read(res, count + 1, fullUrl, ws);
                }, 5000)
            })
        })(res, count, fullUrl, ws)
    })
}